package com.kzd.btgame.main.game

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.createViewTextBottom
import com.kzd.btgame.custom.createViewTextMove
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.IncludeAppRecyclerBinding
import com.kzd.btgame.jetpack.vm.GameViewModel
import com.kzd.btgame.main.game.adapter.GameBargainAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class GameBargainFragment : OwnerViewBindingFragment<IncludeAppRecyclerBinding>() {

    companion object {
        fun newInstance(): GameBargainFragment {
            return GameBargainFragment()
        }
    }

    private val mGameBargainAdapter = GameBargainAdapter()

    private val mViewModel:GameViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRecyclerBinding
        get() = IncludeAppRecyclerBinding::inflate

    override fun initView() {
        mViewBinding.rvAppRecyclerBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGameBargainAdapter
    }

    override fun initData() {
        mViewModel.gameBargain()
    }

    override fun initViewMode() {
        mViewModel.gameBargainLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGameBargainAdapter)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mGameBargainAdapter.setNewInstance(data)
            mGameBargainAdapter.removeAllFooterView()
            if (data.size >= 10) {
                mGameBargainAdapter.addFooterView(mFooterViewMore)
            }
            mGameBargainAdapter.addFooterView(mFooterViewBottom)
        })
    }

    private val mFooterViewMore: View by lazy {
        createViewTextMove(
            context = requireContext(),
            text = "点击查看更多交易>",
            textColor = R.color.colorMain
        ).apply {
            setOnClickListener {

            }
        }
    }

    private val mFooterViewBottom: View by lazy {
        createViewTextBottom(context = requireActivity(),"别撩了！已经到底啦...")
    }
}